<?php

namespace IDock\Insurance\DBIC\Request;

use IDock\IDock\IDockEnums;
use IDock\Insurance\DBIC\DBICEnums;
use IDock\Insurance\DBIC\Response\UploadFileResponse;
use IDock\Visitor\Premium\PremiumInquiryVisitor;

class UploadFileRequest extends AllDBICRequest
{
    protected $route = 'dbic/ecm/images/uploadAndQuoteToQosImage';

    protected $response = UploadFileResponse::class;

    protected $urlTip = '影像上传';

    protected $inLog = false;

    /**
     * @var  PremiumInquiryVisitor $visitor
     */
    protected $visitor;

    protected $inquiryNo;

    /**
     * @param mixed $inquiryNo
     */
    public function setInquiryNo($inquiryNo): void
    {
        $this->inquiryNo = $inquiryNo;
    }

    public function generate(): array
    {
        return [
            'certiNo' => $this->inquiryNo,
            'certiType' => '1',
            'withBase64Prefix' => '1',
            'imageList' => $this->imageList(),
        ];
    }

    private function imageList(): array
    {
        $list = [];
        foreach ($this->visitor->getFileList() as $file) {
            if (!$file['url'] ?? '') {
                continue;
            }

            $imageBase64 = base64_encode(file_get_contents($file['url']));

            $list[] = [
                'imageType' => DBICEnums::$uploadFileType[$file['type']],
                'imageDetailList' => [[
                    'imageName' => IDockEnums::$uploadFileType[$file['type']] . '-' . date('YmdHis') . rand(1000, 9999) . '.jpg',
                    'imageBase64' => $imageBase64,
                ]]
            ];
        }

        return $list;
    }
}
